package com.pengpeng.flash.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pengpeng.flash.common.CustomException;
import com.pengpeng.flash.dto.SetmealDto;
import com.pengpeng.flash.entity.Dish;
import com.pengpeng.flash.entity.DishFlavor;
import com.pengpeng.flash.entity.Setmeal;
import com.pengpeng.flash.entity.SetmealDish;
import com.pengpeng.flash.mapper.SetmealMapper;
import com.pengpeng.flash.service.SetmealDishService;
import com.pengpeng.flash.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 单项-批量删除套餐以及套餐绑定的菜品，,删除之前先判断其售卖状态
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWithDish(String ids) {
        //将前端发送过来的ids以，分隔并放到list集合中
        String[] split = ids.split(",");
        List<String> strings = Arrays.asList(split);
        //循环遍历每一个套餐
        for (String id : strings){
            Setmeal setmeal = this.getById(id);
            //若售卖状态为‘在售’，则删除失败
            if (setmeal.getStatus() != 0){
                //抛出一个自定义业务异常
                throw new CustomException("套餐正在售卖中，请先停售套餐...");
            }
        }
        //删除套餐
        this.removeByIds(strings);
        //删除套餐绑定的菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetmealDish::getSetmealId,strings);
        setmealDishService.remove(queryWrapper);
    }

    /**
     * 新增套餐并且把相应菜品绑定到套餐中
     * @param setmealDto
     */
    @Override
    @Transactional
    public void setmealWithDish(SetmealDto setmealDto) {
        //先保存套餐基本属性
        this.save(setmealDto);
        Long id = setmealDto.getId();

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //遍历循环每一个菜品为他们赋上套餐id并保存
        for (SetmealDish setmealDish : setmealDishes){
            setmealDish.setSetmealId(id);
        }
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 单项-批量起售停售
     * @param status
     * @param ids
     */
    @Override
    @Transactional
    public void updateStatus(Integer status, String ids) {
        String[] split = ids.split(",");
        //循环遍历每一个id
        for (String id :split){
            //根据id查询到相应的套餐
            Setmeal setmeal = this.getById(id);
            //修改其状态
            setmeal.setStatus(status);
            this.updateById(setmeal);
        }
    }

    /**
     * 根据id查询相应套餐和菜品，用于回显到修改页面
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWishDish(String id) {
        //根据id获取对应的套餐信息
        Setmeal setMeal = this.getById(id);
        //新建一个SetmealDto对象，并把套餐的基本属性传给他
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setMeal,setmealDto);

        //根据条件查询出套餐id等于入参位置id的菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        //将菜品集合传入到setmealDto内
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    /**
     * 修改套餐及其绑定的菜品
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWishDish(SetmealDto setmealDto) {
        //修改套餐的基本属性
        this.updateById(setmealDto);

        //移除之前套餐对应的菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        //设置每一个菜品对应的套餐ID
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes){
            setmealDish.setSetmealId(setmealDto.getId());
        }

        setmealDishService.saveBatch(setmealDishes);
    }
}
